package com.monitoringfloty.androidclient;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class SettingsActivity extends Activity {

	private EditText srvIP;
	private EditText deviceIdEdit;
	private SettingsSingleton settings;
	private CheckBox chkMyPos;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		srvIP = (EditText)findViewById(R.id.srvIP);
		deviceIdEdit = (EditText)findViewById(R.id.deviceID);
		chkMyPos = (CheckBox)findViewById(R.id.chkMyPos);
		
		settings = SettingsSingleton.getInstance();
		settings.setContext(getApplicationContext());
		
		srvIP.setText(settings.getString("ServerIP", "192.168.0.30"));
		deviceIdEdit.setText(settings.getString("DeviceID", "AndroidClient"));
		boolean tmp =settings.getBool("ShowMyPosOnMap", false);
		chkMyPos.setChecked(tmp);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.settings, menu);
		return true;
	}
	
    @Override
    public void onBackPressed() {
    	return;
    }
    
    public void SaveBtnClick(View v) {
    	settings.putString("ServerIP", srvIP.getText().toString());
    	settings.putString("DeviceID", deviceIdEdit.getText().toString());
    	settings.putBool("ShowMyPosOnMap", chkMyPos.isChecked());
        Intent returnIntent = new Intent();
        setResult(RESULT_OK,returnIntent);     
        positionUpdater.getInstance().close();
        positionUpdater.getInstance().connect();
        finish();
    	//Toast.makeText(getApplicationContext(), "Save", Toast.LENGTH_SHORT).show();
    }
    
    public void CancelBtnClick(View v) {
    	//Toast.makeText(getApplicationContext(), "Cancel", Toast.LENGTH_SHORT).show();
    	//super.onBackPressed();
    	 Intent returnIntent = new Intent();
    	 //returnIntent.putExtra("result",result);
    	 setResult(RESULT_OK,returnIntent);     
    	 finish();
    }

}
